package com.hitqz.robot.api.business.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hitqz.robot.api.business.component.ExcelSelected;
import com.hitqz.robot.api.business.component.ExcelSelectedResolve;
import com.hitqz.robot.api.business.vo.PatrolResultSummaryVo;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wengyx
 * @date 2024/8/26
 */
public class ExcelUtil {

    public static String getPatrolExcelFilePath(String basePath, Integer ticketsId) {
        String dir = basePath + File.separator + "excel" + File.separator;
        if (!FileUtil.exist(dir)) {
            FileUtil.mkdir(dir);
        }
        return dir + ticketsId + "_finish" + ".xlsx";
    }

    public static String getCvdExcelFilePath(String basePath, String type, String execNo) {
        String dir = basePath + File.separator + "excel" + File.separator;
        if (!FileUtil.exist(dir)) {
            FileUtil.mkdir(dir);
        }
        return dir + type + "_" + execNo + "_finish" + ".xlsx";
    }

    public static void response2Web(HttpServletResponse response, String word) {
        response.reset();
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        try {
            Map<String, Object> map = Maps.newHashMap();
            map.put("code", 0);
            map.put("msg", word);
            map.put("data", null);
            response.getWriter().println(JSON.toJSONString(map));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<List<String>> head(PatrolResultSummaryVo summary) {
        String taskName = summary.getTicketsName();
        String taskTime = DateUtil.format(summary.getStartTime(), "yyyy-MM-dd HH:mm:ss");
        if (summary.getEndTime() != null) {
            String taskEndTime = DateUtil.format(summary.getEndTime(), "yyyy-MM-dd HH:mm:ss");
            taskTime = taskTime + " ~ " + taskEndTime;
        }
        String content = "总检测项数:" + summary.getTotalItemNum()
                + ",正常数:" + summary.getNormalItemNum()
                + ",异常数:" + summary.getAbnormalItemNum()
                + ",识别失败数:" + summary.getVisionFailedItemNum()
                + ",未检测数:" + summary.getWaitItemNum();
        List<List<String>> headTitles = Lists.newArrayList();
        //第一列，1/2/3/4/5/6/7行
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", "任务名称", "任务执行时间", "总结", "序号"));
        //第二列，1/2/3/4/5/6/7行
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "区域名称"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "设备名称"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "部位名称"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "检测项名称"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "检测标准"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "检测时间"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "检测值"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "检测结果"));
        headTitles.add(Lists.newArrayList("智能巡检机器人巡检报告", taskName, taskTime, content, "图片"));
        return headTitles;
    }

    public static List<List<String>> headForSafe() {
        List<List<String>> headTitles = Lists.newArrayList();
        headTitles.add(Lists.newArrayList("安全状态报告", "序号"));
        headTitles.add(Lists.newArrayList("安全状态报告", "安全类型"));
        headTitles.add(Lists.newArrayList("安全状态报告", "发生时间"));
        headTitles.add(Lists.newArrayList("安全状态报告", "异常点位"));
        headTitles.add(Lists.newArrayList("安全状态报告", "事件内容"));
        return headTitles;
    }


    /**
     * 解析表头类中的下拉注解
     *
     * @param head 表头类
     * @param <T>  泛型
     * @return Map<下拉框列索引, 下拉框内容> map
     */
    public static <T> Map<Integer, ExcelSelectedResolve> resolveSelectedAnnotation(Class<T> head) {
        Map<Integer, ExcelSelectedResolve> selectedMap = new HashMap<>();
        Field[] fields = head.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            // 解析注解信息
            ExcelSelected selected = field.getAnnotation(ExcelSelected.class);
            ExcelProperty property = field.getAnnotation(ExcelProperty.class);
            if (selected != null) {
                ExcelSelectedResolve excelSelectedResolve = new ExcelSelectedResolve();
                excelSelectedResolve.setColumnName(StringUtils.join(property.value(), "-"));
                // 处理下拉框内容
                String[] source = excelSelectedResolve.resolveSelectedSource(selected);
                if (source != null && source.length > 0) {
                    excelSelectedResolve.setSource(source);
                    excelSelectedResolve.setFirstRow(selected.firstRow());
                    excelSelectedResolve.setLastRow(selected.lastRow());
                    if (property.index() >= 0) {
                        selectedMap.put(property.index(), excelSelectedResolve);
                    } else {
                        selectedMap.put(i, excelSelectedResolve);
                    }
                }
            }
        }
        return selectedMap;
    }
}
